﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;
using EMR.EMREditor.Src.Document;
using DBUtility;
using CSharpWin;
using SD;

namespace DoctorWorkstation
{
    public partial class FormDoc_Property : DockContent
    {
        private DataTable _symbols;
        private DataTable _imageGallery;
        private DbHelperOra sql_help;

        /// <summary>
        /// 是否允许拖拽操作
        /// </summary>
        private bool m_IsAllowDrop = true;


        public FormDoc_Property(DbHelperOra sqlora)
        {
            InitializeComponent();
            sql_help = sqlora;
            InitBasicElement();
            BindSymbol(Symbols);
        }

        /// <summary>
        /// 基本元素
        /// </summary>
        private void InitBasicElement()
        {
            EMR.EditorUtility.WinControls.OutlookBarItem item;
            item = new EMR.EditorUtility.WinControls.OutlookBarItem("单选", 0, ElementType.SingleElement);
            System.Windows.Forms.ListViewItem listViewItem1 = new System.Windows.Forms.ListViewItem(new string[] {
            item.Text}, 1);
            listViewItem1.Tag = item;

            item = new EMR.EditorUtility.WinControls.OutlookBarItem("多选", 1, ElementType.MultiElement);
            System.Windows.Forms.ListViewItem listViewItem2 = new System.Windows.Forms.ListViewItem(new string[] {
            item.Text}, 2);
            listViewItem2.Tag = item;

            item = new EMR.EditorUtility.WinControls.OutlookBarItem("有无选", 2, ElementType.HaveNotElement);
            System.Windows.Forms.ListViewItem listViewItem3 = new System.Windows.Forms.ListViewItem(new string[] {
            item.Text}, 3);
            listViewItem3.Tag = item;

            item = new EMR.EditorUtility.WinControls.OutlookBarItem("复选框", 1, ElementType.CheckBox);
            System.Windows.Forms.ListViewItem listViewItem4 = new System.Windows.Forms.ListViewItem(new string[] {
            item.Text}, 4);
            listViewItem4.Tag = item;

            item = new EMR.EditorUtility.WinControls.OutlookBarItem("标签", 3, ElementType.FixedText);
            System.Windows.Forms.ListViewItem listViewItem5 = new System.Windows.Forms.ListViewItem(new string[] {
            item.Text}, 5);
            listViewItem5.Tag = item;

            item = new EMR.EditorUtility.WinControls.OutlookBarItem("只读文本", 3, ElementType.Text);
            System.Windows.Forms.ListViewItem listViewItem6 = new System.Windows.Forms.ListViewItem(new string[] {
            item.Text}, 6);
            listViewItem6.Tag = item;

            item = new EMR.EditorUtility.WinControls.OutlookBarItem("定位符", 3, ElementType.Flag);
            System.Windows.Forms.ListViewItem listViewItem7 = new System.Windows.Forms.ListViewItem(new string[] {
            item.Text}, 7);
            listViewItem7.Tag = item;

            item = new EMR.EditorUtility.WinControls.OutlookBarItem("按钮", 3, ElementType.Button);
            System.Windows.Forms.ListViewItem listViewItem8 = new System.Windows.Forms.ListViewItem(new string[] {
            item.Text}, 8);
            listViewItem8.Tag = item;

            item = new EMR.EditorUtility.WinControls.OutlookBarItem("录入提示", 3, ElementType.PromptText);
            System.Windows.Forms.ListViewItem listViewItem9 = new System.Windows.Forms.ListViewItem(new string[] {
            item.Text}, 9);
            listViewItem9.Tag = item;

            item = new EMR.EditorUtility.WinControls.OutlookBarItem("格式化数字", 4, ElementType.FormatNumber);
            System.Windows.Forms.ListViewItem listViewItem10 = new System.Windows.Forms.ListViewItem(new string[] {
            item.Text}, 10);
            listViewItem10.Tag = item;


            item = new EMR.EditorUtility.WinControls.OutlookBarItem("格式化字符串", 5, ElementType.FormatString);
            System.Windows.Forms.ListViewItem listViewItem11 = new System.Windows.Forms.ListViewItem(new string[] {
            item.Text}, 11);
            listViewItem11.Tag = item;

            item = new EMR.EditorUtility.WinControls.OutlookBarItem("格式化日期时间", 5, ElementType.FormatDatetime);
            System.Windows.Forms.ListViewItem listViewItem12 = new System.Windows.Forms.ListViewItem(new string[] {
            item.Text}, 12);
            listViewItem12.Tag = item;

            item = new EMR.EditorUtility.WinControls.OutlookBarItem("月经史公式", 6, ElementType.MensesFormula);
            System.Windows.Forms.ListViewItem listViewItem13 = new System.Windows.Forms.ListViewItem(new string[] {
            item.Text}, 13);
            listViewItem13.Tag = item;

            item = new EMR.EditorUtility.WinControls.OutlookBarItem("水平线", 2, ElementType.HorizontalLine);
            System.Windows.Forms.ListViewItem listViewItem14 = new System.Windows.Forms.ListViewItem(new string[] {
            item.Text}, 14);
            listViewItem14.Tag = item;

            item = new EMR.EditorUtility.WinControls.OutlookBarItem("分页符", 2, ElementType.PageEnd);
            System.Windows.Forms.ListViewItem listViewItem15 = new System.Windows.Forms.ListViewItem(new string[] {
            item.Text}, 15);
            listViewItem15.Tag = item;

            item = new EMR.EditorUtility.WinControls.OutlookBarItem("字典", 0, ElementType.LookUpEditor);
            System.Windows.Forms.ListViewItem listViewItem16 = new System.Windows.Forms.ListViewItem(new string[] {
            item.Text}, 16);
            listViewItem16.Tag = item;

            this.lv_BasicElement.Items.AddRange(new System.Windows.Forms.ListViewItem[] {
            listViewItem1,
            listViewItem2,
            listViewItem3,
            listViewItem4,
            listViewItem5,
            listViewItem6,
            listViewItem7,
            listViewItem8,
            listViewItem9,
            listViewItem10,
            listViewItem11,
            listViewItem12,
            listViewItem13,
            listViewItem14,
            listViewItem15,
            listViewItem16
            });

            imgList.ImageSize = new Size(1, 20);//分别是宽和高
            this.lv_BasicElement.SmallImageList = this.imgList;
        }

        /// <summary>
        /// 特殊字符集合
        /// </summary>
        public DataTable Symbols
        {
            get
            {
                if (_symbols == null)
                {
                    _symbols = sql_help.Query("select  SYMBOL as 名称 from DICT_SYMBOL").Tables[0];
                    _symbols.TableName = "symbols";
                }
                return _symbols;
            }
        }

        /// <summary>
        /// 初始化特殊符号
        /// </summary>
        /// <param name="dataTableSymbol"></param>
        private void BindSymbol(DataTable dataTableSymbol)
        {
            Font font = new System.Drawing.Font("宋体", 10, FontStyle.Regular);
            StringFormat sf = new StringFormat();
            sf.LineAlignment = StringAlignment.Center;
            sf.Alignment = StringAlignment.Center;

            this.lv_Symbol.Items.Clear();

            ImageList list = new ImageList();

            list.ImageSize = new Size(16, 16);
            foreach (DataRow dr in dataTableSymbol.Rows)
            {
                string symbol = dr["名称"].ToString();
                //Bitmap bmp = new Bitmap(list.ImageSize.Width, list.ImageSize.Height);
                //Graphics g = Graphics.FromImage(bmp);
                //g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                //g.DrawString(symbol, font, Brushes.Black, new RectangleF(0, 0, bmp.Width, bmp.Height), sf);
                //g.Dispose();
                Image image = EMR.DoctorWorkstation.Properties.Resources.SymbolPic;
                list.Images.Add(symbol, image);
            }
            lv_Symbol.SmallImageList = list;

            int i = 0;
            foreach (DataRow dr in dataTableSymbol.Rows)
            {
                string symbol = dr["名称"].ToString();
                lv_Symbol.Items.Add(symbol, i);
                i++;
            } 
        }

        /// <summary>
        /// 得到图片数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public byte[] GetImage(string id)
        {
            using (IDataReader reader = sql_help.ExecuteReader("SELECT image FROM imagelibrary WHERE id ='" + id + "' "))
            {
                reader.Read();
                if (!reader.IsDBNull(0))
                {
                    return (byte[])reader.GetValue(0);
                }
            }


            return null;
        }
        /// <summary>
        /// 得到图片列表
        /// </summary>
        public DataTable ImageGallery
        {
            get
            {
                if (_imageGallery == null)
                {
                    //_imageGallery = m_app.SqlHelper.ExecuteDataTable("SELECT ID, G_NAME as 名称 FROM DICT_GALLERY  WHERE (DEPT_ID='" + m_app.User.CurrentDeptId + "' or DEPT_ID='*') and IMG is not null  ORDER BY  FILE_NAME");
                    _imageGallery = sql_help.Query("SELECT id ID, name AS 名称 FROM imagelibrary WHERE image is not null ORDER BY name").Tables[0];
                    _imageGallery.TableName = "image";
                }
                return _imageGallery;
            }
        }

        public PropertyGrid getPropertyGrid()
        {
            return this.propertyGrid_Elment;
        }

        private void lv_BasicElement_DoubleClick(object sender, EventArgs e)
        {
            ListViewEx tree = (ListViewEx)sender;
            DoDragDorpInfo(tree);
        }

        private void lv_BasicElement_ItemDrag(object sender, ItemDragEventArgs e)
        {
            ListViewEx tree = (ListViewEx)sender;
            DoDragDorpInfo(tree);
        }


        private void DoDragDorpInfo(ListViewEx tree)
        {
            if (tree.FocusedItem == null) return;

            if (tree.Name == Set.TOOL__PICTURE)//插入图片
            {
                if (tree.FocusedItem != null)
                {
                    int picID=tree.FocusedItem.ImageIndex;
                    byte[] img = GetImage(picID.ToString());
                    //EMR.EditorUtility.WinControls.OutlookBarItem item = (EMR.EditorUtility.WinControls.OutlookBarItem)tree.FocusedItem.Tag;
                    EMR.EditorUtility.WinControls.OutlookBarItem item = new EMR.EditorUtility.WinControls.OutlookBarItem("ZYTextImage", 3, img);
                    tree.DoDragDrop(item, DragDropEffects.All);

                }
            }
            else if (tree.Name == Set.TOOL__BASICELEMENT)//lv_BasicElement
            {
                if (tree.FocusedItem != null)
                {
                    EMR.EditorUtility.WinControls.OutlookBarItem item = (EMR.EditorUtility.WinControls.OutlookBarItem)tree.FocusedItem.Tag;
                    //tree.DoDragDrop(item, DragDropEffects.All);
                    tree.DoDragDrop(item, DragDropEffects.All);
                }
            }
            else if (tree.Name == Set.TOOL__SIGNITMES)//符号
            {
                if (tree.FocusedItem != null)
                {
                    ListViewItem lbi = this.lv_Symbol.FocusedItem as ListViewItem;
                    EMR.EditorUtility.WinControls.OutlookBarItem item = new EMR.EditorUtility.WinControls.OutlookBarItem(lbi.Text, 3, ElementType.Text);
                    //EMR.EditorUtility.WinControls.OutlookBarItem item = (EMR.EditorUtility.WinControls.OutlookBarItem)tree.FocusedItem.Tag;
                    tree.DoDragDrop(item, DragDropEffects.All);
                }
            }

        }

        /// <summary>
        /// 当属性发生变化时
        /// </summary>
        /// <param name="s"></param>
        /// <param name="e"></param>
        private void propertyGrid_Elment_PropertyValueChanged(object s, PropertyValueChangedEventArgs e)
        {
            OnPropetyValueChanged(new EventOjbArgs());
        }

        //事件委托
        public event EventHandler<EventOjbArgs> PropertyValueChanged;

        private void OnPropetyValueChanged(EventOjbArgs args)
        {
            if (PropertyValueChanged != null)
            {
                PropertyValueChanged(this, args);
            }

        }

        private void lv_Symbol_ItemDrag(object sender, ItemDragEventArgs e)
        {

            ListViewEx tree = (ListViewEx)sender;
            DoDragDorpInfo(tree);
        }

        /// <summary>
        /// 当伸展时，加载医学图片信息。
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void xpPanel6_PanelStateChange(object sender, EventArgs e)
        {
            UIComponents.XPPanel xpPanel = (UIComponents.XPPanel)sender;
            if(xpPanel.PanelState == UIComponents.XPPanelState.Expanded&&_imageGallery == null)
            {
                foreach (DataRow row in ImageGallery.Rows)
                {
                   string symbol = row["名称"].ToString();
                   string picID = row["ID"].ToString();
                   lv_Picture.Items.Add(symbol, Convert.ToInt32(picID));
                }
            }
        }
        /// <summary>
        /// 图片呢拖动
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void lv_pic_ItemDrag(object sender, ItemDragEventArgs e)
        {
            ListViewEx tree = (ListViewEx)sender;
            DoDragDorpInfo(tree);
        }
    }
}
